package cn.com.cmbc.utils;


import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class HTTPUtil {
    private static final Logger LOG = LoggerFactory.getLogger(HTTPUtil.class);
    private static final OkHttpClient client = new OkHttpClient();
    public static String get(String url) {
        // 验证 URL 格式
        if (url == null || !url.startsWith("http://") && !url.startsWith("https://")) {
            throw new IllegalArgumentException("Invalid URL format: " + url);
        }

        LOG.info("HTTP GET request to URL: {}", url);

        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            if (response.body() != null) {
                String json = response.body().string();
                LOG.info("接口返回结果：{}", json);
                return json;
            }
        } catch (IOException e) {
            LOG.error("Error during HTTP GET request: {}", e.getMessage(), e);
            return "{\"status\":\"failed\"}";
//            return "Error: " + e.getMessage();
        }

        return "{\"status\":\"unknown\"}";



    }


}
